package cn.hchaojie.javase.week2;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.junit.Test;

public class CharCounter {
	@Test
	public void test() {
		for (char c = 'A'; c <= 'Z'; c++) {
			System.out.println(c + " " + (int) c);
		}

		for (char c = 'a'; c <= 'z'; c++) {
			System.out.println(c);
		}

		System.out.println((char) 66);

		// 大写转小写
		char upperCase = 'Q';
		// char lowerCase = (char) ('Q' + ('a' - 'A'));
		char lowerCase = (char) ('Q' + 32);
		System.out.println(lowerCase);
	}

	@Test
	public void test2() {
		Map<Character, Integer> map = new HashMap<>();

		map.put('Q', 5);
		map.put('A', 3);
		map.put('J', 8);
		map.put('G', 51);

		System.out.println(map);
	}

	@Test
	public void test3() {
		Map<Character, Integer> map = new TreeMap<>();

		map.put('Q', 5);
		map.put('A', 3);
		map.put('J', 8);
		map.put('G', 51);

		System.out.println(map);
	}

	/**
	 * 1. 给定一个字符串，在控制台打印26个字母出现的次数，（大小写一起统计）。打印的时候要求从a到z的顺序打印。
	 */
	@Test
	public void test4() {
		String str = "piano playing, swimming, tennis, and research In neuropsychology";

		int[] counts = new int[26]; // 0 -> 'a', 1 -> 'b', ..., 25 -> 'z'

		// char c = 'p'; ==> 'p' - 'a' counts[c - 'a']++;
		for (int i = 0; i < str.length(); i++) {
			// 如果不是字母, continue
			char c = str.charAt(i);

			// 'A' <= c && c <= 'Z' 'a' <= c && c <= 'z'
			boolean isUpperCase = ('A' <= c && c <= 'Z');
			boolean isLowerCase = ('a' <= c && c <= 'z');
			boolean isLetter = isUpperCase || isLowerCase;
			if (!isLetter) {
				continue;
			}

			// 如果是字母，作统计
			if (isUpperCase) { // 大写字母
				c = (char) (c + 32); // 大写转小写
			}

			int index = (c - 'a');
			counts[index]++;
		}

		for (int i = 0; i < counts.length; i++) {
			char current = (char) ('a' + i);

			if (counts[i] > 0) {
				System.out.println(current + " - " + counts[i]);
			}
		}
	}
	
	@Test
	public void test6() {
//		Map<Character, Integer> map = new HashMap<>();
//		
//		map.put('a', 1);
//		
//		int count = map.get('a');
//		count++;
//		map.put('a', count);
//		
//		System.out.println(map.get('a'));
		
		for (char c = 'a'; c <= 'z'; c++) {
			System.out.println(Character.valueOf(c).hashCode());
		}
	}

	@Test
	public void test5() {
		String str = "piano playing, swimming, tennis, and research In neuropsychology";

		Map<Character, Integer> map = new HashMap<>();

		for (int i = 0; i < str.length(); i++) {
			// 如果不是字母, continue
			char c = str.charAt(i);

			// 'A' <= c && c <= 'Z' 'a' <= c && c <= 'z'
			boolean isUpperCase = ('A' <= c && c <= 'Z');
			boolean isLowerCase = ('a' <= c && c <= 'z');
			boolean isLetter = isUpperCase || isLowerCase;
			if (!isLetter) {
				continue;
			}

			// 如果是字母，作统计
			if (isUpperCase) { // 大写字母
				c = (char) (c + 32); // 大写转小写
			}

			if (map.containsKey(c)) {
				// int count = map.get(c);
				// count++;
				map.put(c, map.get(c) + 1);
			} else {
				map.put(c, 1);
			}
		}

		for (Entry<Character, Integer> entry : map.entrySet()) {
			System.out.println(entry.getKey() + " - " + entry.getValue());
		}
	}
}
